local fangcan = fk.CreateSkill{
  name = "mod__jfangcan",
  frequency = Skill.Compulsory,
}
local U = require "packages/utility/utility"
fangcan:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and not player.dead then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local ids = {}
      if not turn_event then return end
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
        return false
      end, turn_event.id)
      ids = table.filter(ids, function (id)
        local c = Fk:getCardById(id)
        return room:getCardArea(id) == Card.DiscardPile and ((c.type == Card.TypeTrick and c:isCommonTrick()) or c.trueName == "slash")
      end)
      local ids2 = {}
      if #ids > 0 then
        local common = table.filter(ids, function(id)
          local c = Fk:getCardById(id)
          return c.type == Card.TypeTrick and c:isCommonTrick()
          end)
        local damage = table.filter(ids, function(id)
          local c = Fk:getCardById(id)
          return (c.type == Card.TypeTrick and c.is_damage_card) or c.trueName == "slash"
          end)
        if #common == 1 then 
          table.insertIfNeed(ids2, common[1]) 
        end
        if #damage == 1 then 
          table.insertIfNeed(ids2, damage[1]) 
        end
      end
      if #ids2 > 0 then
        self.cost_data = ids2
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cids = self.cost_data
    local use = U.askForUseRealCard(room, player, cids, ".", self.name, "#fangcan_use-ask", {expand_pile = cids}, true)
    if use then
      local card = Fk:cloneCard(use.card.name)
      use.card = card
      room:useCard(use)
    else
      local cid = cids[1]
      if #cids > 1 then
        cid = room:askForCard(player, 1, 1, false, self.name, false, ".|.|.|^(hand,equip)", nil, cids)[1]
      end
      room:obtainCard(player, cid, true, fk.ReasonPrey)
    end
  end,
})
Fk:loadTranslationTable{
  ["mod__fangcan"] = "纺残",
  [":mod__fangcan"] = "锁定技，每回合结束时，你获得或视为使用本回合弃牌堆中唯一的普通锦囊牌或唯一的伤害牌。",
  ["#fangcan_use-ask"] = "纺残：使用一张牌，或点击取消获得其中一张",
}
return fangcan